package com.dongdongshop.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.model.TbTypeTemplate;
import com.dongdongshop.service.SpecOptionService;
import com.dongdongshop.service.TemplateService;
import com.dongdongshop.vo.SpecOptionVo;
import com.dongdongshop.vo.SpecVo;
import com.dongdongshop.vo.TemplateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("option")
public class SpecOptionController {

    @Autowired
    private SpecOptionService specOptionService;

    @Autowired
    private TemplateService templateService;


    @PostMapping("guige")
    public Result optionList(@RequestBody SpecVo vo) {
        List<SpecVo> voList = specOptionService.optionList(vo);
        return Result.OK().setData(voList);
    }

    @PostMapping("TbSpecificationshowbyid")
    public Result TbSpecificationshow(@RequestParam Long id) {
        try {
            //获取模板信息
            TbTypeTemplate tbTypeTemplate = templateService.showbyid(id);
            String specIds = tbTypeTemplate.getSpecIds();
            //将模板类中specIds字段转成对象
            List<SpecOptionVo> tbSpecificationOptionVOS = (List<SpecOptionVo>) JSONUtils.parse(specIds);
            for (SpecOptionVo optionVO:tbSpecificationOptionVOS) {
                SpecOptionVo showecho = specOptionService.showecho(optionVO.getId());
                optionVO.setRows(showecho.getRows());
            }
            TemplateVo templetVO = new TemplateVo();
            BeanUtils.copyProperties(tbTypeTemplate,templetVO);
            templetVO.setTbSpecificationOptionVOSList(tbSpecificationOptionVOS);
            return Result.response(ResultEnums.ECHO_SUCCESS).setData(templetVO);
        } catch (Exception e) {
            return Result.response(ResultEnums.ECHO_ERROR);
        }
    }
}
